package com.example.administrator.ygddriver.order.outorder;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseFragment;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.into.login.driverjoin.small.smallmessage.SmallMessageActivity;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.map.SendMapActivity;
import com.example.administrator.ygddriver.order.getorder.PickUpRequestBean;
import com.example.administrator.ygddriver.order.specific.SpecificActivity;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.UrlValuse;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import java.util.HashMap;

public class OutOrderFragment extends BaseFragment {
    private OutOrderAdapter mOutOrderAdapter;
    private RecyclerView recyclerView;
    private String c_token, c_driver;
    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();
    private String city, district, street;
    private String mDriverPhone, mMyPhone;

    @Override
    public int setLayout() {
        return R.layout.fragment_out_order;
    }

    @Override
    public void initView(View view) {

        recyclerView = view.findViewById(R.id.fragment_out_order_rv);
        mOutOrderAdapter = new OutOrderAdapter(R.layout.item_fragment_out_order, null);
        View v = LayoutInflater.from(context).inflate(R.layout.item_specific_adapter_empty, null);
        mOutOrderAdapter.setEmptyView(v);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(mOutOrderAdapter);


    }

    @Override
    public void initData() {
        LiteOrmTools.getInformation(UserBean.class, context, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                c_token = data.getC_token();
                c_driver = data.getC_id();
                mDriverPhone = data.getC_account();
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        mLocationClient = new LocationClient(context);
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        setLocationClientOption();
        mLocationClient.start();
        mOutOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ToSendRequestBean.ResultBean resultBean = (ToSendRequestBean.ResultBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.item_fragment_out_order_status_ll:
                        if (resultBean.getC_status().equals("3")) {
                            Intent intent1 = new Intent(context, SendMapActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putParcelable("outOrderBean", resultBean);
                            bundle.putInt("tag", 2);
                            intent1.putExtras(bundle);
                            startActivityForResult(intent1, 0x002);
                        }
                        break;
                    case R.id.item_fragment_out_order_phone_iv:
                        initSecretaxbHtml(mDriverPhone, resultBean.getC_account(), c_token);
                        break;
                }
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x002 && resultCode == 0x001) {
            mOutOrderAdapter.setNewData(null);
            mLocationClient.start();
        }
    }

    //号码隐私保护
    private void initSecretaxbHtml(String c_numbera, String c_numberb, String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_numbera", c_numbera);
        hashMap.put("c_numberb", c_numberb);
        hashMap.put("c_type", UrlValuse.TYPE);
        hashMap.put("c_token", c_token);
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE + UrlValuse.SECRETAXB, hashMap, SecretaxbRequestBean.class, new OkGoRequests.OnTrue<SecretaxbRequestBean>() {
            @Override
            public void hasData(SecretaxbRequestBean date) {
                if (date.isSuccess()) {
                    Intent myCallIntent = new Intent(Intent.ACTION_DIAL,
                            Uri.parse("tel:" + date.getResult().getC_secretno()));
                    startActivity(myCallIntent);
                }
            }
        }, new OkGoRequests.OnError<SecretaxbRequestBean>() {
            @Override
            public void noData() {

            }
        });
    }


    //司机端送件列表查询
    private void initToSendHtml(String c_driver, String c_customerlatitude, String c_customerlongitude, String limit, String offset, String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_driver", c_driver);
        hashMap.put("c_customerlatitude", c_customerlatitude);
        hashMap.put("c_customerlongitude", c_customerlongitude);
        hashMap.put("limit", limit);
        hashMap.put("offset", offset);
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", "2");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_DRIVER + UrlValuse.TO_SEND, hashMap, ToSendRequestBean.class, new OkGoRequests.OnTrue<ToSendRequestBean>() {
            @Override
            public void hasData(ToSendRequestBean date) {
                if (date.isSuccess()) {
                    mOutOrderAdapter.setPlace(city + district + street);
                    mOutOrderAdapter.setNewData(date.getResult());
                }
            }
        }, new OkGoRequests.OnError<ToSendRequestBean>() {
            @Override
            public void noData() {

            }
        });
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            double latitude = location.getLatitude();    //获取纬度信息
            double longitude = location.getLongitude();    //获取经度信息

            city = location.getCity();    //获取城市
            district = location.getDistrict();    //获取区县
            street = location.getStreet();    //获取街道信息
            int errorCode = location.getLocType();
            //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
            if (errorCode == 161 || errorCode == 61) {
                mLocationClient.stop();
                initToSendHtml(c_driver, String.valueOf(latitude), String.valueOf(longitude), "20", "1", c_token);
            } else {
//                Toast.makeText(context, "定位失败", Toast.LENGTH_SHORT).show();
            }

        }
    }

    private void setLocationClientOption() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        option.setOpenGps(true);
        option.setLocationNotify(true);
        option.setIgnoreKillProcess(false);
        option.SetIgnoreCacheException(false);
        option.setEnableSimulateGps(false);
        mLocationClient.setLocOption(option);
        option.setIsNeedAddress(true);
        option.setIsNeedLocationPoiList(true);

    }
}
